package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.RedisConstant;
import com.itheima.dao.SetmealDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.Setmeal;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
import redis.clients.jedis.JedisPool;

import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhouwei
 * @date 2020-01-02 11:21
 */
@Service(interfaceClass = SetmealService.class)
@Transactional
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealDao setmealDao;
    @Autowired
    private JedisPool jedisPool;
    @Autowired
    private FreeMarkerConfigurer freeMarkerConfigurer;
    @Value("${out_put_path}")
    private String outPutPath;//从属性文件中读取要生成的html对应的目录

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        String queryString = queryPageBean.getQueryString();
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        //调用分页助手
        PageHelper.startPage(currentPage,pageSize);//设置当前页码，每页条数
        Page<CheckGroup> page=setmealDao.findPage(queryPageBean);//条件查询
        long total = page.getTotal();//总页码
        List<CheckGroup> result = page.getResult();//所选页信息
        PageResult pageResult=new PageResult(total,result);
        return pageResult;
    }

    /**
     * 查询检查组
     * @return
     */
    @Override
    public List<CheckGroup> findCheckGroup() {
        List<CheckGroup>list=setmealDao.findCheckGroup();
        return list;
    }


    /**
     * 添加setmeal
     * @param setmeal
     * @param checkgroupIds
     */
    @Override
    public void add(Setmeal setmeal, Integer[] checkgroupIds) {
        //先添加t_setmeal表，获取lastid
        setmealDao.addSetmeal(setmeal);
        //添加关联表
        for(Integer id:checkgroupIds){
            HashMap<String,Integer> map=new HashMap<>();
            map.put("setmeal_id",setmeal.getId());
            map.put("checkgroup_id",id);
            setmealDao.addSetmealcheckgroup(map);
         }
         //数据库操作完成后，将图片保存到redis
        jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES,setmeal.getImg());
        //新增套餐后需要生成的静态页面
       generateMobileStaticHtml();
    }

    //
    public void generateMobileStaticHtml(){
        //查询所有套餐
        List<Setmeal> setmealList = this.findAll();
        //生成套餐静态页面
        generateMobileSetmealListHtml(setmealList);
        //生成页面详情静态页面
        generateMobileSetmealDetailHtml(setmealList);
    }

    //生成套餐静态页面
    public void generateMobileSetmealListHtml(List<Setmeal> setmealList){
        Map<String,Object> map = new HashMap<>();
        map.put("setmealList",setmealList);
        this.generateHtml("mobile_setmeal.ftl","m_setmeal.html",map);
    }
    //生成页面详情静态页面
    public void generateMobileSetmealDetailHtml(List<Setmeal> setmealList){
        for (Setmeal setmeal : setmealList) {
            Map map=new HashMap<>();
            map.put("setmeal",setmealDao.findById(setmeal.getId()));
            this.generateHtml("mobile_setmeal_detail.ftl","setmeal_detail_"+setmeal.getId()+".html",map);
        }
    }

    //生成静态页面
    public void generateHtml(String templateName, String htmlPageName, Map map){
        //创建模板对象
        Configuration configuration = freeMarkerConfigurer.getConfiguration();
        Writer out;
        try {
            //加载模板文件
            Template template = configuration.getTemplate(templateName);
            //构造输出流
            out=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(outPutPath+"/"+htmlPageName))));
            //输出文件到指定位置
            template.process(map,out);
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 查询所有套餐
     * @return
     */
    @Override
    public List<Setmeal> findAll() {
        List<Setmeal> setmeals=setmealDao.findAll();
        return setmeals;
    }

    /**
     * 查询单个套餐
     * @param id
     * @return
     */
    @Override
    public Setmeal findById(int id) {
        Setmeal setmeal=setmealDao.findById(id);
        return setmeal;
    }

    /**
     * 查询套餐统计数据
     * @return
     */
    @Override
    public List<Map<String,Object>> findSetmealCount() {
        List<Map<String,Object>> list=setmealDao.findCount();
        return list;
    }
}
